﻿using System;
using System.Html;
using enyo;
using onyx;

namespace JenkinsView
{
    public class ServerListView : enyo.FittableRows, IObserver
    {
        [NoCompile, EvalAtCompile]
        private const string LST_SVR = "lstSvr";
        [NoCompile, EvalAtCompile]
        private const string ITM_SVR = "itmSvr";
        [NoCompile, EvalAtCompile]
        private const string LBL_SVR = "lblSvr";
        [NoCompile, EvalAtCompile]
        private const string LBL_URL = "lblUrl";

        protected new string Classes = CssConst.ONYX;

        private new object[] Components = new Control[]
        {
            new Toolbar() { Kind = onyx.ComponentKind.Toolbar, LayoutKind = enyo.LayoutKind.FittableColumnsLayout, NoStretch = true,
                Components = new Component[]
                {
                    new Control(){ Content = "Jenkins Server List", Fit = true, Classes = CssConst.TOOLBAR_TITLE },
                    new onyx.IconButton(){ Kind = onyx.ComponentKind.IconButton, Src = ImgSrc.PLUS, ontap = "showServerEditor"},
                }
            },

            new List() { Kind = enyo.ComponentKind.List, Name = LST_SVR, Fit = true, Touch = true, OnSetupItem = "setupItem",
                Components = new Component[]
                {
                    new Control(){ Name = ITM_SVR, Classes = CssConst.PANELS_ITEM, NoStretch = true, LayoutKind = enyo.LayoutKind.FittableColumnsLayout,
                        Components = new Component[]
                        {
                            new Control() { Classes = CssConst.WIDTH_90, ontap = "serverSelected",
                                Components = new Component[]
                                {
                                    new Control() { Name = LBL_SVR, Classes = CssConst.PANELS_CONTENT},
                                    new Control() { Name = LBL_URL, Classes = CssConst.URL_VAL }
                                }
                            },                            
                            new onyx.IconButton() { Kind = onyx.ComponentKind.IconButton, Src = ImgSrc.DELETE, ontap = "deleteServer" }
                        }
                    }
                }
            }
        };

        private C FindControl<C>(string name) where C : class
        {
            return this._[name] as C;
        }

        public override void Create()
        {
            this.Inherited(Arguments.Value);
            DomainModel.Model.ServerManager.SetObserver(this);
        }

        private void SetupItem(Control c, SetupRowEventArgs arg)
        {
            var svr = DomainModel.Model.ServerManager.ListServers()[arg.Index];
            var lblSvr = this.FindControl<Control>(LBL_SVR);
            lblSvr.Content = svr.SvrName;
            var lblUrl = this.FindControl<Control>(LBL_URL);
            lblUrl.Content = svr.Url;

            var item = this.FindControl<Control>(ITM_SVR);
            var list = this.FindControl<List>(LST_SVR);
            item.AddRemoveClass(CssConst.PANELS_ITEM_SELECTED, list.IsSelected(arg.Index));
        }

        private void ShowServerEditor()
        {
            DomainModel.Model.ServerManager.RequestAppendServer();
        }

        private void DeleteServer(Control c, EnyoEventArgs arg)
        {
            int idx = (int)arg["index"];

            DomainModel.Model.ServerManager.RequestRemoveServer(idx);
        }

        private void ServerSelected(Control c, EnyoEventArgs arg)
        {
            int idx = (int)arg["index"];
            Console.Log("Get Server Data: " + idx);
            DomainModel.Model.ServerManager.SelectedServerIndex = idx;
            DomainModel.Model.JobManager.ReadJobsData(false);
        }

        public void Update(int reason, object arg)
        {
            switch (reason)
            {
                case DomainModel.SVR_LIST_CHANGE:
                    Console.Log("Update ServerListView, reason:" + reason);
                    var lst = this.FindControl<List>(LST_SVR);
                    lst.Count = (int)DomainModel.Model.ServerManager.ListServers().Length;
                    lst.Refresh();
                    break;
            }
        }
    }
}
